﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinServiceFileListener.Extensions
{
    public static class StringExtension
    {
        /// <summary>
        ///  是否包含字符串
        /// </summary>
        /// <param name="value">字符串值</param>
        /// <param name="strContains">输出过滤的字符串名</param>
        /// <param name="filterStrs">过滤的字符串名</param>
        /// <returns>true：输出过滤的字符串名；false：表示不含过滤的字符串</returns>
        public static bool IsContains(this string value, ref string strContains, params string[] filterStrs)
        {
            strContains = "";
            for (int i = 0; i < filterStrs.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(filterStrs[i]))
                {
                    continue;
                }
                if (value.Contains(filterStrs[i]))
                {
                    strContains = filterStrs[i];
                    return true;
                }
                else
                {
                    strContains += $"、{filterStrs[i]}";
                }

            }
            strContains = strContains.RemoveStartStr();
            return false;
        }
        /// <summary>
        ///  是否包含字符串
        /// </summary>
        /// <param name="value">字符串值</param>
        /// <param name="strContains">输出过滤的字符串名</param>
        /// <param name="filterStr">过滤的字符串名（以空格隔开）</param>
        /// <returns>true：输出过滤的字符串名；false：表示不含过滤的字符串</returns>
        public static bool IsContains(this string value, ref string strContains, string filterStr)
        {
            string[] s = filterStr.Split(' ');
            return value.IsContains(ref strContains, s);
        }
        /// <summary>
        /// 比较字符串是否相等
        /// </summary>
        /// <param name="value">字符串值</param>
        /// <param name="strEquals">输出过滤的字符串名</param>
        /// <param name="strs">过滤的字符串名（以空格隔开）</param>
        /// <returns>true：输出过滤的字符串名；false：表示不含过滤的字符串</returns>
        public static bool IsEquals(this string value, ref string strEquals, params string[] strs)
        {
            strEquals = "";
            foreach (var str in strs)
            {
                if (string.IsNullOrWhiteSpace(str))
                {
                    continue;
                }
                if (value.Equals(str))
                {
                    strEquals = str;
                    return true;
                }
            }
            strEquals = strEquals.RemoveStartStr();
            return false;
        }
        /// <summary>
        /// 比较字符串是否相等
        /// </summary>
        /// <param name="value">字符串值</param>
        /// <param name="strEquals">输出过滤的字符串名</param>
        /// <param name="str">过滤的字符串名（以空格隔开）</param>
        /// <returns>true：输出过滤的字符串名；false：表示不含过滤的字符串</returns>
        public static bool IsEquals(this string value, ref string strEquals, string str)
        {
            string[] s = str.Split(' ');
            return value.IsEquals(ref strEquals, s);
        }



        #region 私有方法
        /// <summary>
        /// 移除掉最开始的符号值
        /// </summary>
        /// <param name="value">字符串数据源</param>
        /// <param name="StartStr">最开始的符号（字符串长度为1）</param>
        /// <returns>移除后的字符串</returns>
        private static string RemoveStartStr(this string value, string StartStr = "、")
        {
            if (!string.IsNullOrEmpty(value) && value.StartsWith(StartStr))
            {
                value = value.Substring(1, value.Length - 1); // 去掉第一个字符串
            }
            return value;
        }
        #endregion
    }
}
